/*    
 * Copyright (c) 2014 ISoftStone, Inc.
 * All right reserved.
 *
 * 文件名：      UserDaoImpl.java
 * 作者:     Jacky Wang
 * 创建日期： 2014-1-1 下午03:15:51
 * 版本：           
 *
 */
package com.isoftstone.industry.aaa.core.dao.assist.impl;

import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.isoftstone.industry.common.util.Constants;
import com.isoftstone.industry.persistance.bean.User;
import com.isoftstone.industry.aaa.core.dao.assist.UserDaoPlus;
import com.isoftstone.modules.core.utils.DateUtils;

/**
 *
 * @author jacky
 */
public class UserDaoImpl implements UserDaoPlus
{
	@PersistenceContext 
	private EntityManager em; 
	
	@Override
	public List<User> findNeedAccountingUsers()
	{
//		String hql = "select distinct u from ProductInstance x inner join x.user as u where x.status !=?1";
		String hql = "select distinct u from ProductInstance x inner join x.user as u where u.type !=?1 and (u.status =?2 or u.status=?3)";
		Query query = em.createQuery(hql);
		query.setParameter(1, Constants.CUS_USER_TYPE_BUSINESS_MEMBER);
		query.setParameter(2, Constants.CUS_USER_STATUS_ACTIVATE);
		query.setParameter(3, Constants.CUS_USER_STATUS_SUSPENDED);
		return query.getResultList();
	}

	@Override
	public boolean hasChargingProductInst(String userId, Date checkDate) {
		String hql = "select count(*) from ProductInstance x where x.user.id=? and x.startDate<=?";
		Query query = em.createQuery(hql);
		query.setParameter(1, userId);
		Date param2 = DateUtils.string2Date(DateUtils.date2String(checkDate, DateUtils.YYYY_MM_DD_PATTERN) + " 23:59:59", DateUtils.YYYY_MM_DD_HH_MM_SS_PATTERN);
		query.setParameter(2, param2);
		int count = ((Long)query.getSingleResult()).intValue();
		return count != 0;
	}
		
}
